package com.hedl.testClass;

import java.io.*;

/**
 * @author Big Watermelon
 * @description
 * @date 2024/02/13/08:46
 */
public class Employee implements Serializable {
    public String name;
    public String address;
    public transient int SSN;
    public int number;
    public void mailCheck(){
        System.out.println("Mailing a check to "+ name +" "+address);
    }

    public static void main(String[] args) {
        // 序列化对象
        /*
        Employee employee = new Employee();

        employee.name = "大西瓜";
        employee.address = "火星";
        employee.SSN = 22;
        employee.number = 100;

        try(FileOutputStream fos = new FileOutputStream("/Users/a101/Pictures/桌面/javafile/employee.ser");
            ObjectOutputStream oos = new ObjectOutputStream(fos)) {
            oos.writeObject(employee);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
         */

        //反序列化
        Employee employee = null;
        try(FileInputStream fis = new FileInputStream("/Users/a101/Pictures/桌面/javafile/employee.ser");
        ObjectInputStream ois = new ObjectInputStream(fis)){
            employee = (Employee) ois.readObject();

            System.out.println("查看是否反序列化成功!");
            System.out.println(employee.name);
            System.out.println(employee.address);
            System.out.println(employee.SSN);
            System.out.println(employee.number);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}
